// lib/api/community_api.dart
import 'package:dio/dio.dart';

import '../models/community.dart'; // 导入 Community 模型
import '../models/history_list_response.dart';
import '../models/response.dart'; // 导入通用响应模型
import 'dio_client.dart'; // 导入 Dio 客户端

class HistoryApi {
  final Dio _dio = DioClient().dio; // 获取 Dio 单例

  /// 获取用户历史记录列表
  Future<ResponseSuccess<HistoryListResponse>> getHistoryList({
    int page = 1,
    int size = 10,
  }) async {
    try {
      final response = await _dio.get(
        '/user/history',
        queryParameters: {
          'page': page,
          'size': size,
        },
      );

      return ResponseSuccess.fromJson(
        response.data,
            (json) => HistoryListResponse.fromJson(json as Map<String, dynamic>),
      );
    } on DioException catch (e) {
      final errorMessage = e.response?.data?['msg'] ?? '获取历史记录失败';
      throw 'Error $errorMessage (Status: ${e.response?.statusCode})';
    } catch (e) {
      throw '获取历史记录发生未知错误: $e';
    }
  }
}